package com.tanhua.server.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.*;
import com.tanhua.exception.BusinessException;
import com.tanhua.interceptor.UserHolder;
import com.tanhua.model.domain.Announcement;
import com.tanhua.model.domain.ErrorResult;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.vo.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MessagesService {
    @DubboReference
    private UserApi userApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @DubboReference
    private FriendApi friendApi;
    @DubboReference
    private AnnouncementApi announcementApi;
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @DubboReference
    private Commentapi commentapi;

    public UserInfoVo findUserInfoByHuanxin(String huanxinId) {
        //1、根据环信id查询用户
        String userId = huanxinId.replaceAll("HX", "");
        UserInfo userInfo = userInfoApi.findById(Long.valueOf(userId));
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, userInfoVo);//copy同名同类型的属性
        //userinforvo和 userinfor age属性类型不同  手动set
        if (userInfo.getAge() != null) {
            userInfoVo.setAge(userInfo.getAge());
        }

        return userInfoVo;
    }

    //添加好友
    public void contacts(Long friendId) {
        //1、将好友关系注册到环信
        Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + UserHolder.getUserId(),
                Constants.HX_USER_PREFIX + friendId);
        if (!aBoolean) {
            throw new BusinessException(ErrorResult.error());
        }
        //2、如果注册成功，记录好友关系到mongodb
        friendApi.save(UserHolder.getUserId(), friendId);

    }

    public PageResult findFriends(Integer page, Integer pagesize, String keyword) {
        //1、调用API查询当前用户的好友数据 -- List<Friend>
        List<Friend> list = friendApi.findByUserId(UserHolder.getUserId(), page, pagesize);
        if (CollUtil.isEmpty(list)) {
            return new PageResult();
        }
        //2、提取数据列表中的好友id
        List<Long> userIds = CollUtil.getFieldValues(list, "friendId", Long.class);
        //3、调用UserInfoAPI查询好友的用户详情
        UserInfo info = new UserInfo();
        info.setNickname(keyword);
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, info);
        //4、构造VO对象
        List<ContactVo> vos = new ArrayList<>();
        for (Friend friend : list) {
            UserInfo userInfo = map.get(friend.getFriendId());
            if (userInfo != null) {
                ContactVo vo = ContactVo.init(userInfo);
                vos.add(vo);
            }
        }
        return new PageResult(page, pagesize, 0l, vos);
    }

    //公告分页查询
    public PageResult findAnnouncements(Integer page, Integer pagesize) {
        Page pages = announcementApi.findAnnouncements(page, pagesize);
        List<Announcement> list = pages.getRecords();
        List<Announcementvo> nlist = new ArrayList<>();
        for (Announcement announcement : list) {
            Announcementvo vo = Announcementvo.init(announcement);
            nlist.add(vo);
        }
        return new PageResult(page, pagesize, 0l, nlist);
    }

    public PageResult Lovesdetails(Integer page, Integer pagesize) {
        //先根据用户id查询出该用户的所有评论信息
        Long userId = UserHolder.getUserId();
        List<Comment> commentList = commentapi.findcommentById(userId, page, pagesize, CommentType.LOVE);
        if(CollUtil.isEmpty(commentList)){
            return new PageResult();
        }
        //2、提取数据列表中的评论人Id
        List<Long> userIds = CollUtil.getFieldValues(commentList, "userId", Long.class);
        Map<Long, UserInfo> infoMap = userInfoApi.findByIds(userIds, null);
        List<CommentVo> vos = getCommentVos(commentList, infoMap);
        return new PageResult(page, pagesize, 0l, vos);
    }

    //抽取方法封装CommentVo方法
    private List<CommentVo> getCommentVos(List<Comment> commentList, Map<Long, UserInfo> infoMap) {
        List<CommentVo> vos = new ArrayList<>();
        for (Comment comment : commentList) {
            UserInfo userInfo = infoMap.get(comment.getUserId());
            if (userInfo != null) {
                CommentVo vo = CommentVo.init(userInfo, comment);
                vos.add(vo);
            }
        }
        return vos;
    }

    public PageResult commentsdetails(Integer page, Integer pagesize) {
        //先根据用户id查询出该用户的所有评论信息
        Long userId = UserHolder.getUserId();
        List<Comment> commentList = commentapi.findcommentById(userId, page, pagesize, CommentType.COMMENT);
        if(CollUtil.isEmpty(commentList)){
            return new PageResult();
        }
        //2、提取数据列表中的评论人Id
        List<Long> userIds = CollUtil.getFieldValues(commentList, "userId", Long.class);
        Map<Long, UserInfo> infoMap = userInfoApi.findByIds(userIds, null);
        List<CommentVo> vos = getCommentVos(commentList, infoMap);
        return new PageResult(page, pagesize, 0l, vos);
    }

    public PageResult likesdetails(Integer page, Integer pagesize) {

        //先根据用户id查询出该用户的所有评论信息
        Long userId = UserHolder.getUserId();
        List<Comment> commentList = commentapi.findcommentById(userId, page, pagesize, CommentType.LIKE);
        if(CollUtil.isEmpty(commentList)){
            return new PageResult();
        }
        //2、提取数据列表中的评论人Id
        List<Long> userIds = CollUtil.getFieldValues(commentList, "userId", Long.class);
        Map<Long, UserInfo> infoMap = userInfoApi.findByIds(userIds, null);
        List<CommentVo> vos = getCommentVos(commentList, infoMap);
        return new PageResult(page, pagesize, 0l, vos);
    }
}
